home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / stk-3.002 / stk-3 / STk-3.1 / Contrib / Python / jredford-mail.txt
Encoding:
Text File  |  1994-06-11  |  4.4 KB  |  176 lines

  1. Herafter is an excerpt of a mail of jredford@lehman.com about Python/STk mail
  2. I hope it will suffice to build a running system (I had no time to test it).
  3. ------------------
  4.  
  5.  
  6. This is the file I added.. python-stk.c:
  7.  
  8.  
  9. #include <stk.h>
  10. #include <Py/allobjects.h>
  11. #include <Py/pythonrun.h>
  12. #include <Py/import.h>
  13.  
  14. PRIMITIVE python_init(void)
  15. {
  16.   initall();
  17.   return UNDEFINED;
  18. }
  19.  
  20. PRIMITIVE python(SCM string)
  21. {
  22.   run_command(CHARS(string));
  23.   return UNDEFINED;
  24. }
  25.  
  26. SCM convert(object *o)
  27. {
  28.   if is_intobject(o) {
  29.     return makeinteger(getintvalue(o));
  30.   } else {
  31.     if is_floatobject(o) {
  32.       return makenumber(getfloatvalue(o));
  33.     } else {
  34.       if is_stringobject(o) {
  35.     return makestrg(getstringsize(o),getstringvalue(o));
  36.       } else {
  37.     if is_tupleobject(o) {
  38.       int i,j = gettuplesize(o);
  39.       SCM vec;
  40.       vec = makevect(j,makeinteger(0));
  41.       for (i = 0; i < j; i++) {
  42.         vector_set(vec, makeinteger(i), convert(gettupleitem(o,i)));
  43.       }
  44.       return vector2list(vec);
  45.     } else {
  46.       if is_listobject(o) {
  47.         int i,j = getlistsize(o);
  48.         SCM vec;
  49.         vec = makevect(j,makeinteger(0));
  50.         for (i = 0; i < j; i++) {
  51.           vector_set(vec, makeinteger(i), convert(getlistitem(o,i)));
  52.         }
  53.         return vector2list(vec);
  54.       }
  55.     }
  56.       }
  57.     }
  58.   }
  59.   return UNDEFINED;
  60. }
  61.  
  62. PRIMITIVE python_value(SCM dict, SCM var)
  63. {
  64.   object *m, *d, *v;
  65.   m = add_module(CHARS(dict));
  66.   if (m == NULL)
  67.     return UNDEFINED;
  68.   d = getmoduledict(m);
  69.   v = dictlookup(d,CHARS(var));
  70.   return convert(v);
  71. }
  72.  
  73. char *
  74. getprogramname()
  75. {
  76.         return "stk";
  77. }
  78.  
  79.  
  80. END of file python-stk.c
  81.  
  82. The initall() should be done in init_python-stk(). You might know of a
  83. better way to convert the data types, or to more efficiently make
  84. lists. These were the calls that I found easilly. I also didnt know
  85. how to typecheck the arguments passed. They should all be strings.
  86.  
  87.  
  88. This is the summary of what I added to primitives.c
  89. /*
  90.  *
  91.  * p r i m i t i v e s . c            -- List of STk subrs
  92.  */
  93.  
  94. #ifdef USE_PYTHON
  95. extern PRIMITIVE python_init(void);
  96. extern PRIMITIVE python(SCM string);
  97. extern PRIMITIVE python_value(SCM dict, SCM var);
  98. #endif
  99.  
  100. static struct Primitive Scheme_primitives[] = { 
  101.  .
  102.  .
  103.  .
  104.  
  105. #ifdef USE_PYTHON
  106.   {"python-init",           tc_subr_0,          python_init},
  107.   {"python",                tc_subr_1,          python},
  108.   {"python-value",          tc_subr_2,          python_value},
  109. #endif
  110.  .
  111.  .
  112.  .
  113.  
  114.  
  115.  
  116.  
  117. An example.. well, I never used Tk before Stk, so Im not gonna do
  118. anything too pretty.
  119.  
  120. #!/opt/stk/bin/stk -file
  121.  
  122. (python-init)  ;This should really be (require "python") or (require "python-stk")
  123. (python "import posix") ;Python is a module based language
  124.  
  125. (button ".b" :text "Press me" :borderwidth 2 :background "slate gray" :foreground "gold"
  126.     :command '(begin
  127.             (python "a = posix.listdir('.')")
  128.             (for-each (lambda (a) (.f.lb 'insert 99999 a))
  129.                   (python-value "__main__" "a"))))
  130.  
  131. (frame ".f")
  132. (listbox ".f.lb" :foreground "grey40" :background "grey70" :yscroll ".f.s 'set")
  133. (scrollbar ".f.s" :relief "sunken" :command ".f.lb 'yview")
  134.  
  135. (pack .f.lb .f.s :side "right" :fill "y")
  136. (pack .b .f)
  137.  
  138.  
  139.  
  140. Where this would populate a listbox with the filenames in the current
  141. directory, everytime the button was pushed. posix.listdir() returns a
  142. python 'list' type. __main__ is the name/alias of the primary module.
  143. If I could figure out STk calling enough to use variable args, I'd
  144. have made (python-value "a") assume a value of "__main__" for the
  145. dict..
  146.  
  147.  
  148.  
  149. Makefile modifications:
  150.  
  151. # This isnt the default python dir, just what I use. should be a
  152. # configure option.. --with-python=/opt/python 
  153. PYTHONDIR     = /opt/python
  154. PYTHONLIBPATH = $(PYTHONDIR)/lib/python/lib
  155. PYTHONCFLAGS  = -I$(PYTHONDIR)/include -I$(PYTHONDIR)/include/Py
  156. PYTHONOBJ     = python-stk.o
  157. PYTHONLIB     = $(PYLIBPATH)/libModules.a \
  158.                 $(PYLIBPATH)/libPython.a \
  159.                 $(PYLIBPATH)/libParser.a \
  160.                 $(PYLIBPATH)/libObjects.a
  161. PYTHONLIBS    = -lreadline -ltermcap
  162. LIBS          = -lnsl -ldl $(PYTHONLIBS) -lm
  163.  
  164.  
  165.  
  166. ...
  167.         /bin/rm -f primitives.o
  168.         make CFLAGS="$(CFLAGS) -DUSE_PYTHON" primitives.o
  169.         $(CC) -o stkp-bin  -DUSE_PYTHON $(OBJ) $(PYTHONOBJ) -I$(PYTHONDIR)/include/Py -DNO_MAIN $(PYTHONLIBPATH)/config.c userinit.c $(ALLIBS) $(XLIBSW) $(PYTHONLIB) $(LIBS)
  170.         /bin/rm -f primitives.o
  171.  
  172.  
  173. Removing primitives.c to recompile with -DUSE_PYTHON is a horrible
  174. kludge, as I am sure you agree. The key thing is to also compile in
  175.    -DNO_MAIN $(PYLIBPATH)/config.c
  176.